package Others.Stream;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author 赵超
 * @Description 工作生活中的学习随笔 - Stream流
 * @create 2017-04-14 14:47
 **/
public class One {
    private static int i = 0;
    static {
        /*Runnable runnable = () -> {
            // task to run goes here
            System.out.println("呵呵 " + ++i);
            if (i == 10) System.exit(0);
        };

        ScheduledExecutorService service = Executors
                .newSingleThreadScheduledExecutor();
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间，第四个是时间单位
        service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);*/
    }
    public static void main(String[] args) {

        String[] arrayString = {"4", "2", "6", "1", "3", "9", "6", "1"};

        Map<Integer, Integer> collect = Arrays.asList(arrayString).stream()
                .map(Integer::new) // 把String类型的数值转换为数字
                .filter(i -> i % 2 == 0) // 过滤出偶数
                .collect(Collectors.groupingBy(p -> p, Collectors.summingInt(p -> 1))); // 收集每个偶数的值和相应的出现次数
        System.out.println(collect);

        List<Integer> collect1 = Arrays.asList(arrayString).stream()
                .map(Integer::new)
                .filter(i -> i % 2 == 0)
                .collect(Collectors.toList()); // 将过滤出的偶数传入集合列表
        System.out.println(collect1);

        Integer reduce = Arrays.asList(arrayString).stream()
                .map(Integer::new)
                .filter(i -> i % 2 == 0)
                .reduce(0, (a, b) -> a + b); // 将过滤出的偶数求和
        System.out.println(reduce);

        /**
         * 去重复
         */
        List<String> list = Arrays.stream(arrayString).distinct().collect(Collectors.toList());
        System.out.println(list);

        /**
         * 跳过前N个元素，取得之后的元素
         */
        List<String> list1 = Arrays.stream(arrayString).skip(3).collect(Collectors.toList());
        System.out.println(list1);

        /**
         * 取得前N个元素
         */
        List<String> list2 = Arrays.stream(arrayString).limit(4).collect(Collectors.toList());
        System.out.println(list2);

        /**
         * 使用filterMap，去掉相同字符
         */
        String[] words = {"Hello", "Word"};
        Arrays.stream(words).map(w -> w.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList()).forEach(System.out::println);


        /**
         * 找出能和指定数字满足勾股定理的数字
         */
        int a = 9;
        IntStream.range(1, 100)
                .filter(x -> Math.sqrt(x * x + a * a) % 1 == 0)//判断整数
                .boxed()//装箱
                .map(x -> new int [] {a, x, (int) Math.sqrt(a * a + x * x)})
                .forEach(r -> System.out.println(r[0] + " " + r[1] + " " + r[2]));

        IntStream.range(1, 100)
                .filter(x -> Math.sqrt(x * x + a * a) % 1 == 0)//判断整数
                .mapToObj(x -> new int [] {a, x, (int) Math.sqrt(a * a + x * x)})
                .forEach(r -> System.out.println(r[0] + " " + r[1] + " " + r[2]));



    }
}
